// pages/myself/coupon/MyCoupon.js
let network = null;
let utils = null;
let api = null;
const app = getApp();
var Zan = require('../../../dist/index.js');
let WxNotificationCenter = require('../../../utils/WxNotificationCenter.js');
var start01 = 0;
var start02 = 0;
var start03 = 0;
Page(Object.assign({}, Zan.TopTips, Zan.Toast, Zan.Tab,{

  /**
   * 页面的初始数据
   */
  data: {
    tab: {
      list: [{
        id: 0,
        title: '未使用'
      }, {
        id: 1,
        title: '已使用'
        }, {
          id: 2,
          title: '已过期'
        }],
      selectedId: 0,
      scroll: false
    },
    nodata: {
      msg: '暂无数据',
      notype: 1,
      imgurl: '/images/nodatas.png',
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();

    // 注册列表刷新事件
    WxNotificationCenter.addNotification("RefreshMyCoupon", this.refreshNotification, this)

    this.data.tab.selectedId = options.index;
    this.setData({
      tab: this.data.tab,
    })
    this.setData({
      activeIndex: options.index
    })

    this.onPullDownRefresh()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    WxNotificationCenter.removeNotification("RefreshMyCoupon", this)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    if (this.data.tab.selectedId == 0) {
      start01 = 0;
      this.setData({
        items: []
      })
      network._post_page_json(this, api.myCouponList, start01, {status: 0 }, success00);
    } else if (this.data.tab.selectedId == 1){
      start02 = 0;
      this.setData({
        items: []
      })
      network._post_page_json(this, api.myCouponList, start02, { status: 1 }, success00);
    } else {
      start03 = 0;
      this.setData({
        items: []
      })
      network._post_page_json(this, api.myCouponList, start03, { status: 3 }, success00);
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 进入优惠券详情
   */
  goUseCoupon: function (e) {
    wx.navigateTo({
      url: '/pages/myself/coupon/UseCoupon?couponId=' + e.currentTarget.dataset.id
    })
  },
  handleZanTabChange(e) {
    var componentId = e.componentId;
    var selectedId = e.selectedId;

    this.setData({
      [`${componentId}.selectedId`]: selectedId
    })

    this.setData({
      activeIndex: selectedId
    })

    if (this.data.tab.selectedId == 0) {
      start01 = 0;
      this.setData({
        items: []
      })
      network._post_page_json(this, api.myCouponList, start01, { status: 0 }, success00);
    } else if (this.data.tab.selectedId == 1){
      start02 = 0;
      this.setData({
        items: []
      })
      network._post_page_json(this, api.myCouponList, start01, { status: 1 }, success00);
    } else {
      start03 = 0;
      this.setData({
        items: []
      })
      network._post_page_json(this, api.myCouponList, start01, { status: 3 }, success00);
    }

  },

  refreshNotification: function () {
    start03 = 0;
    this.setData({
      items: []
    })
    network._post_page_json(this, api.myCouponList, 0, { status: 0 }, success00);
  }
}))

var success00 = function(that,res){
  if (res.data.data) {
    that.setData({
      items: res.data.data
    })
  }
}